package cn.maihe.elg.operation.exception;

import cn.maihe.elg.operation.model.enums.InnerRespCode;

/**
 * @Description
 * @Author WJH
 * @Date 2021/04/15
 */
public class CenterException extends RuntimeException {

    private InnerRespCode innerRespCode;
    private String message;

    public CenterException(String message, Throwable throwable, InnerRespCode innerRespCode) {
        super(throwable);
        this.innerRespCode = innerRespCode;
        this.message = isEmpty(message) ? innerRespCode.getRespDesc() : message;
    }

    public static boolean isEmpty(final CharSequence cs) {
        return cs == null || cs.length() == 0;
    }

    public static CenterException error(String message, Throwable throwable) {
        return of(InnerRespCode.FAILURE, message, throwable);
    }

    public static CenterException exception(String message, Throwable throwable) {
        return of(InnerRespCode.EXCEPTION, message, throwable);
    }

    public static CenterException timeout(String message, Throwable throwable) {
        return of(InnerRespCode.TIMEOUT, message, throwable);
    }


    public static CenterException of(InnerRespCode innerRespCode, String message, Throwable throwable) {
        return new CenterException(message, throwable, innerRespCode);
    }

    public CenterException message(String message) {
        this.message = message;
        return this;
    }

    public InnerRespCode getInnerRespCode() {
        return innerRespCode;
    }

    @Override
    public String getMessage() {
        return message;
    }

}
